//
//  LZSResultViewController.m
//  IOSProject
//
//  Created by lzs on 2018/4/20.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSResultViewController.h"
#import "LZSResultModel.h"
#import "LZSResultCell.h"

#import "LZSCaipiaoQueryModel.h"

@interface LZSResultViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *realArray;
@property (nonatomic, strong) NSDictionary *dataArray;
@end

@implementation LZSResultViewController

-(NSDictionary *)dataArray{
    if (!_dataArray) {
        _dataArray = @{
            @"lottery_id": @"ssq",
            @"lottery_name": @"双色球",
            @"lottery_res": @"03,08,11,14,18,23,16",
            @"lottery_no": @"18028",
            @"lottery_date": @"2018-03-13",
            @"lottery_exdate": @"2018-05-11",
            @"lottery_sale_amount":@"352,015,830",
            @"lottery_pool_amount": @"578,094,167",
            @"lottery_prize": @[
                              @{
                                  @"prize_name": @"一等奖",
                                  @"prize_num": @"7",
                                  @"prize_amount": @"7,676,997",
                                  @"prize_require": @"6+1"
                              },
                              @{
                                  @"prize_name": @"二等奖",
                                  @"prize_num": @"220",
                                  @"prize_amount": @"106,471",
                                  @"prize_require": @"6+0"
                              },
                              @{
                                  @"prize_name": @"三等奖",
                                  @"prize_num": @"1869",
                                  @"prize_amount": @"3,000",
                                  @"prize_require": @"5+1"
                              },
                              @{
                                  @"prize_name": @"四等奖",
                                  @"prize_num": @"82708",
                                  @"prize_amount": @"200",
                                  @"prize_require": @"5+0,4+1"
                              },
                              @{
                                  @"prize_name": @"五等奖",
                                  @"prize_num": @"1493643",
                                  @"prize_amount": @"10",
                                  @"prize_require": @"4+0,3+1"
                              },
                              @{
                                  @"prize_name": @"六等奖",
                                  @"prize_num": @"8341565",
                                  @"prize_amount": @"5",
                                  @"prize_require": @"2+1,1+1,0+1"
                              }
                              ]
        };
    }
    return _dataArray;
}
-(NSMutableArray *)realArray{
    if (!_realArray) {
        _realArray = [NSMutableArray array];
    }
    return _realArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor lightGrayColor];
    //
    self.tableView.estimatedRowHeight = 80;
    
    self.tableView.separatorColor = [UIColor colorWithHexString:@"#c8c8c8"];
    
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    //
    self.tableView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(0);
    }];
    
    //    [self headerSdcycleScrollView];
    
    //
    [self.tableView.mj_header beginRefreshing];
//    [self lotterType];
}

-(void)refresh{
    //    sleep(0.5);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
//    [self lotterType];
    [self caipiaoQuery];
}

-(void)loadMore{
    //    sleep(2);
//    [self.tableView.mj_header endRefreshing];
//    [self.tableView.mj_footer endRefreshing];
}

-(void)lotterType{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"91b22ffb918dbb3a2b5f98be27009a47" forKey:@"key"];
    [params setValue:self.lottery_id forKey:@"lottery_id"];
    [self postData:@"http://apis.juhe.cn/lottery/query" param:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
//        NSLog(@"%@",responseObject);
        [self.realArray removeAllObjects];
        
        LZSResultModel *datamodel = [LZSResultModel mj_objectWithKeyValues:responseObject[@"result"]];
//    LZSResultModel *datamodel = [LZSResultModel mj_objectWithKeyValues:self.dataArray];
    
        if (datamodel.lottery_prize.count<20) {
            self.tableView.mj_footer.hidden = YES;
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    
        [self.realArray addObjectsFromArray:datamodel.lottery_prize];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        NSLog(@"%@",error);
    }];
}

-(void)caipiaoQuery{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"31771dfbcb6cda3d" forKey:@"appkey"];
    [params setValue:self.lottery_id forKey:@"caipiaoid"];
    [self getData:@"http://api.jisuapi.com/caipiao/query" param:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        //        NSLog(@"%@",responseObject);
        [self.realArray removeAllObjects];
        
//        LZSResultModel *datamodel = [LZSResultModel mj_objectWithKeyValues:responseObject[@"result"]];
        //    LZSResultModel *datamodel = [LZSResultModel mj_objectWithKeyValues:self.dataArray];
        LZSCaipiaoQueryModel *datamodel = [LZSCaipiaoQueryModel mj_objectWithKeyValues:responseObject[@"result"]];
        
//        //是刷新还是加载
//        if (self.start==0) {
//            if (datamodel.list.count==0) {
//                //                self.noDataView.hidden = NO;
//                self.tableView.hidden = YES;
//            }else{
//                //                self.noDataView.hidden = YES;
//                self.tableView.hidden = NO;
//            }
//
//            [self.realArray removeAllObjects];
//            [self.realArray addObjectsFromArray:datamodel.list];
//        }else{
//            [self.realArray addObjectsFromArray:datamodel.list];
//        }
//
//        //是否提示可加载
//        if (datamodel.list.count<10) {
//            // 变为没有更多数据的状态
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//            self.tableView.mj_footer.hidden = YES;
//        }else{
//            self.start++;
//        }
        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView reloadData];
//        });
        if (datamodel.prize.count ==0) {
            self.nodataView.hidden = NO;
            self.tableView.hidden = YES;
        }else{
            self.nodataView.hidden = YES;
            self.tableView.hidden = NO;
            if (datamodel.prize.count<20) {
                self.tableView.mj_footer.hidden = YES;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.realArray addObjectsFromArray:datamodel.prize];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        NSLog(@"%@",error);
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.realArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LZSResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[LZSResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
//    cell.model = self.realArray[indexPath.row];
    cell.queryModel = self.realArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
